package com.mjee.action.struts2;

import java.util.List;

import com.mjee.entity.User;
import com.mjee.service.UserService;
import com.mtoolkit.page.Page;

public class UserAction extends AbstractAction {
	
	private String userName;
	private String password;
	private int pageIndex = DEFAULT_PAGE_INDEX;
	private int pageSize = DEFAULT_PAGE_SIZE;
	private UserService userService;
	private List<User> userList;
	
	public String regist() {
		User newUser = new User();
		newUser.setUserName(userName);
		newUser.setPassword(password);
		userService.addUser(newUser);
		
		return SUCCESS;
	}
	
	public String login() {
		User user = userService.findUser(userName, password);
		
		if (user == null) {
			return FAIL;
		}
		
		user.setUserName(userName);
		user.setPassword(password);
		getSession().put(USER_SESSION_KEY, user);
		
		return SUCCESS;
	}
	
	public String logout() {
		getSession().remove(USER_SESSION_KEY);
		
		return SUCCESS;
	}
	
	public String userList() {
		Page page = new Page(pageIndex, pageSize);
		userList = userService.queryUsers(page);
		
		return SUCCESS;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public List<User> getUserList() {
		return userList;
	}

}
